/**
 * Created by luomin on 16/4/7.
 */
'use strict';

angular.module('xianwan')
	.factory('LocalStorage', function () {

		return {

			save: function (key, value) {
				/* localStorage超额将无法登录 */
				window.localStorage.setItem(key, JSON.stringify(value));
			},

			get: function (key) {
				try {
					return JSON.parse(window.localStorage.getItem(key));
				} catch (e) {
					return '';
				}
			}, 

			remove: function (key) {
				window.localStorage.removeItem(key);
			},

			add: function (key, value) {
				let self = this;

				let val = self.get(key);
				if (angular.isArray(val)) {
					val.push(value);

					self.save(key, val);
				}
			}, 

			getUserVal: function (key, userId) {
				let self = this;
				let item = self.get(key) || {};

				return item[userId];
			}, 

			addUserVal: function (key, userId, val) {
				let self = this;
				let item = self.get(key) || {};
				item[userId] = val;

				self.save(key, item);
			}, 

		}

	});